package com.xbts.system.module.payment.kit.params.impl;

import com.xbts.common.enums.ShoppingCarEnums;
import com.xbts.common.exception.ServiceException;
import com.xbts.system.domain.CnBalanceOrder;
import com.xbts.system.enums.PayStatusEnum;
import com.xbts.system.module.payment.enums.CashierEnum;
import com.xbts.system.module.payment.kit.dto.PayParam;
import com.xbts.system.module.payment.kit.dto.PaymentSuccessParams;
import com.xbts.system.module.payment.kit.params.CashierExecute;
import com.xbts.system.module.payment.kit.params.dto.CashierParam;
import com.xbts.system.module.payment.service.ICnWalletLogService;
import com.xbts.system.service.ICnBalanceOrderService;
import com.xbts.system.service.ICnWeChatTemplateMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 订单支付信息获取
 *
 * @author Chopper
 * @since 2021-01-25 20:00
 */
@Slf4j
@Component
public class OrderCashier implements CashierExecute {

    /**
     * 设置
     */
    @Autowired
    private ICnWalletLogService cnWalletLogService;
    /**
     * 余额订单
     */
    @Autowired
    private ICnBalanceOrderService cnBalanceOrderService;
    @Autowired
    private ICnWeChatTemplateMsgService cnWeChatTemplateMsgService;

    @Override
    public CashierEnum cashierEnum() {
        return CashierEnum.ORDER;
    }

    @Override
    public CashierParam getPaymentParams(PayParam payParam) {
        if (payParam.getOrderType().equals(CashierEnum.ORDER.name())) {
            //准备返回的数据
            CashierParam cashierParam = new CashierParam();
            //订单信息获取
            CnBalanceOrder order = cnBalanceOrderService.queryDetail(payParam.getSn());

//            如果订单已支付，则不能发起支付
            if (order.getPayStatus().equals(PayStatusEnum.PAID)) {
                throw new ServiceException("订单已支付，不能再次进行支付");
            }
            // 订单超时
            if (order.getPayStatus().equals(PayStatusEnum.CANCEL)) {
                throw new ServiceException("当前订单已超时，请重新充值");
            }
            cashierParam.setPrice(order.getAmount().doubleValue());

            try {
//                BaseSetting baseSetting = JSONUtil.toBean(settingService
//                        .get(SettingEnum.BASE_SETTING.name()).getSettingValue(), BaseSetting.class);
//                cashierParam.setTitle(baseSetting.getSiteName());
                cashierParam.setTitle("余额充值");
            } catch (Exception e) {
                cashierParam.setTitle("余额充值");
            }
//            List<OrderItem> orderItemList = order.getOrderItems();
//            StringBuilder subject = new StringBuilder();
//            for (OrderItem orderItem : orderItemList) {
//                subject.append(orderItem.getGoodsName()).append(";");
//            }

//            cashierParam.setDetail(subject.toString());
//            Setting setting = settingService.get(SettingEnum.PAY_NAME.name());
//            PayNameSetting ossSetting =  JSONUtil.toBean(setting.getSettingValue(), PayNameSetting.class);
//            cashierParam.setDetail(ossSetting.getName());
            cashierParam.setDetail("支付名称");

            cashierParam.setOrderSns(payParam.getSn());
//            cashierParam.setCreateTime(order.getOrder().getCreateTime());
            cashierParam.setCreateTime(new Date());
            return cashierParam;
        }

        return null;
    }

    @Override
    public void paymentSuccess(PaymentSuccessParams paymentSuccessParams) {

        PayParam payParam = paymentSuccessParams.getPayParam();
        if (payParam.getOrderType().equals(CashierEnum.ORDER.name())) {
            // 支付成功逻辑处理
            CnBalanceOrder cnBalanceOrder = cnBalanceOrderService.paymentSuccess(payParam.getSn());
            log.info("订单{}支付成功,金额{},方式{}", payParam.getSn(),
                    paymentSuccessParams.getPaymentMethod(),
                    paymentSuccessParams.getReceivableNo());

            //  更新用户余额
            cnWalletLogService.updateUserBalanceAndLog(cnBalanceOrder.getUserId(),
                    BigDecimal.valueOf(paymentSuccessParams.getPrice()),
                    paymentSuccessParams.getPaymentMethod(), paymentSuccessParams.getReceivableNo(), ShoppingCarEnums.RECHARGE, paymentSuccessParams.getReceivableNo(), null);
            // 发送公众号消息
            cnWeChatTemplateMsgService.sendReminderOfSuccessfulRechargeMsg(cnBalanceOrder.getUserId().toString(), cnBalanceOrder);
        }
    }

    @Override
    public Boolean paymentResult(PayParam payParam) {
        if (payParam.getOrderType().equals(CashierEnum.ORDER.name())) {
            CnBalanceOrder cnBalanceOrder = cnBalanceOrderService.queryDetail(payParam.getSn());
            return PayStatusEnum.PAID.equals(cnBalanceOrder.getPayStatus());
        }
        return false;
    }
}
